package com.woniu.gateway.config;

import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayFlowRule;
import com.alibaba.csp.sentinel.adapter.gateway.common.rule.GatewayRuleManager;
import com.alibaba.csp.sentinel.datasource.ReadableDataSource;
import com.alibaba.csp.sentinel.datasource.nacos.NacosDataSource;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.csp.sentinel.datasource.Converter;

import java.util.HashSet;
import java.util.List;
import java.util.Set;
import com.alibaba.fastjson2.JSON;
@Configuration
public class SentinelGatewayDataSourceConfiguration {

    @Autowired
    private Converter<String, Set<GatewayFlowRule>> gatewayFlowRuleConverter;

    @PostConstruct
    public void loadGatewayRules() {
        ReadableDataSource<String, Set<GatewayFlowRule>> nacosDataSource =
                new NacosDataSource<>("43.142.246.250:8848", "DEFAULT_GROUP",
                        "cloud-gateway-sentinel-gateway-flow",
                        gatewayFlowRuleConverter);

        GatewayRuleManager.register2Property(nacosDataSource.getProperty());
        System.out.println(">>> Sentinel Gateway 加载规则（完成）");
    }

}